package com.wjk.cms.service.impl;

import com.wjk.cms.dao.OrderRepository;
import com.wjk.cms.pojo.Order;
import com.wjk.cms.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WUGN
 * @des
 * @date 2021/9/13 15:47
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Override
    public void save(Order order) {
        orderRepository.save(order);
    }

    @Override
    public Order getByCode(String code) {
        return orderRepository.getByCode(code);
    }

    @Override
    public List<Order> find(String userId,String type, Integer pageNo, Integer pageSize) {
        if(pageNo==null || pageNo<1) {
            return orderRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();



                if (StringUtils.isNotEmpty(userId)) {
                    predicates.add(cb.equal(root.get("userId"), userId));
                }
                if (StringUtils.isNotEmpty(type)) {
                    predicates.add(cb.equal(root.get("type"), type));
                }


                return cb.and(predicates.toArray(new Predicate[0]));
            }, Sort.by(Sort.Order.desc("createTime")));
        }else {
            return orderRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();


                if (StringUtils.isNotEmpty(userId)) {
                    predicates.add(cb.equal(root.get("userCode"), userId));
                }

                if (StringUtils.isNotEmpty(type)) {
                    predicates.add(cb.equal(root.get("type"), type));
                }
                return cb.and(predicates.toArray(new Predicate[0]));
            }, PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime")).getContent();
        }
    }

    @Override
    public List<Order> customerOrderlist(String customerId,String keywords, Integer pageNo, Integer pageSize) {
        if(pageNo==null || pageNo<1) {
            return orderRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(keywords)) {
                    predicates.add(cb.like(root.get("code"),"%"+keywords+"%"));

                }

                if (StringUtils.isNotEmpty(customerId)) {
                    predicates.add(cb.equal(root.get("customerId"), customerId));
                }




                return cb.and(predicates.toArray(new Predicate[0]));
            }, Sort.by(Sort.Order.desc("createTime")));
        }else {
            return orderRepository.findAll((root, query, cb) -> {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(keywords)) {
                    predicates.add(cb.like(root.get("code"),"%"+keywords+"%"));

                }
                if (StringUtils.isNotEmpty(customerId)) {
                    predicates.add(cb.equal(root.get("customerId"), customerId));
                }

                return cb.and(predicates.toArray(new Predicate[0]));
            }, PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "createTime")).getContent();
        }
    }

    @Override
    public Order getByCardId(Long cardId) {
        return orderRepository.getByCardId(cardId);
    }


}
